package com.report.controller;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.report.dto.request.MemberReq;
import com.report.dto.request.MemberRuleReq;
import com.report.dto.request.OrderReq;
import com.report.dto.response.MemberRes;
import com.report.dto.response.MemberRuleRes;
import com.report.dto.response.OrderRes;
import com.report.entity.Member;
import com.report.entity.Rule;
import com.report.service.IMemberRuleService;
import com.report.service.IMemberService;
import com.report.service.IOrderService;
import com.zmh.framework.config.BaseCommonInterface;
import com.zmh.framework.constants.Constants;
import com.zmh.framework.util.MD5;
import com.zmh.framework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author summer
 * @since 2024/06/25 14:54:50
 * 会员
 */
@RestController
@RequestMapping("/member")
@AllArgsConstructor
public class MemberController implements BaseCommonInterface<MemberReq, MemberRes> {

    private final IMemberService memberService;

    @Override
    public Result<String> save(@Valid MemberReq memberReq) {
        String msg="更新成功";
        Member convert = Convert.convert(Member.class, memberReq);
        if(memberReq.getRuleId()-1>0){
            convert.setMemberState(1);
        }
        if(ObjectUtil.isNotEmpty(memberReq.getId())){
            memberService.updateMemberInfo(memberReq);
        }else{
            memberService.saveMember(convert);
            msg="默认密码123456";
        }

        return Result.successResult(msg,null);
    }

    @Override
    public Result<String> del(Integer id) {
        memberService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<MemberRes>> lists(MemberReq memberReq) {
        return Result.successResult(memberService.lists(memberReq));
    }

    @Override
    public Result<MemberRes> findById(Integer integer) {
        return null;
    }

    @Override
    public Result<String> batchDel(List<MemberRes> list) {
        return null;
    }

    private final IMemberRuleService ruleService;

    @GetMapping(value = "/memberRuleLists")
    public Result<List<MemberRuleRes>>  memberRuleLists() {
        List<Rule> list = ruleService.list();
        return Result.successResult(Convert.toList(MemberRuleRes.class, list));
    }
}
